package com.adv.ems.config;

import com.adv.ems.model.dto.LoginInfo;
import com.adv.ems.utils.JwtUtil;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.servlet.HandlerInterceptor;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Component
public class RequestInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String token = request.getHeader("token");
        if (StringUtils.isEmpty(token)) {
            return HandlerInterceptor.super.preHandle(request, response, handler);
        }
        try {
            LoginInfo loginVO = JwtUtil.verifyToken(token);
            JwtUtil.threadLocal.set(loginVO);
        }catch (Exception e){

        }
        return HandlerInterceptor.super.preHandle(request, response, handler);
    }
}
